package com.witframework.components.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * tree jsp tag
 * <pre>
 * 属性name:tree name
 * treeBox:tree div box name
 * theme:tree的外观，缺省值为：csh_vista，可选值:{csh_bluebooks,csh_bluefolders,csh_books,sch_scbrblue,csh_winstyle,chs_yellowbooks}
 * rootId:跟节点id
 * childAction:获取子节点数据的servlet
 * enableCheckBoxes:checkBox是否有效
 * onClickHandler:单击节点时JavaScript的动作
 * onOpenHandler:打开节点时JavaScript的动作
 * onCheckHandler:检查节点时JavaScript的动作
 * onDblClickHandler:双击击节点时JavaScript的动作
 * </pre><P>
 * <blockquote><pre><code>
 * 树示例
 * &lt;div id="demoTreeBox" style="width:400; height: 400"&gt;&lt;/div&gt;
 * &lt;ws:tree name="demoTree" treeBox="demoTreeBox" rootId="0"
 * 	childAction="/wfes/demo/demoTree.action"
 * 	enableCheckBoxes="1" onClickHandler="doOnClick" /&gt;
 *
 * serlvet(示例为spring controller)代码如下：
 *  public class DemoTreeController extends BaseMultiActionController {
 *  	protected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {
 *  	TreeNode treeNode = new TreeNode();
 *  	treeNode.setRootNode(new TreeItem(request.getParameter("id"), ""));
 *  	Random rdm = new Random();
 *  	int id = rdm.nextInt();
 *  	TreeItem item = new TreeItem("id1" + id, "节点1-" + id);
 *  	item.setChild(1);
 *  	item.setOpen(true);
 *  	for (int i = 0; i < 10; i++) {
 *  		TreeItem childItem = new TreeItem("id1X" + i + id, "节点1X" + i + "-"+ id);
 *  		childItem.setChild(1);
 *  		item.addChildItem(childItem);
 *  		childItem = new TreeItem("id1Y" + id, "节点1Y-" + id);
 *  		childItem.setNocheckbox(false);
 *  		item.addChildItem(childItem);
 *  	}
 *  	item.addUserdata(new TreeUserdata("data","user data"));
 *   	treeNode.addItem(item);
 *   	renderString(response,treeNode.outString());
 *   	return null;
 *   }
 *  }
 * </code></pre></blockquote>
 *
 * @see com.witframework.web.tree.TreeItem
 * @see com.witframework.web.tree.TreeNode
 * @see com.witframework.web.tree.TreeUserdata
 * @author wuxinyang
 *
 */
public class TreeTag extends TagSupport{
	//tree name
	private String name;
	//tree div box name
	private String treeBox;
	//tree theme
	private String theme="csh_winstyle";
	//跟节点名称
	private String rootId;
	
	private String data;
	private String data2;
	private String datatype;
	private String parentid;
	private String nodeid;
	private String nodeid2;
	private String nodelabel;
	private String nodelabel2;
	private String others;
	//获取子节点数据的action
	private String childAction;
	//checkBox是否有效
	//点击节点时JavaScript的动作
	private String onClickHandler=null;
	private String onOpenHandler=null;
	private String onCheckHandler=null;
	private String onDblClickHandler=null;
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		String contextPath=request.getContextPath()+"/webresource/components/tree/";
		StringBuffer treeHtml
			=new StringBuffer("<script>\n"+name+"=new dhtmlXTreeObject(\""+treeBox+"\",\"1000\",\"90%\",\""+rootId+"\");\n");
		treeHtml.append(name+".setImagePath(\""+contextPath+"imgs/"+theme+"/\");\n");
		treeHtml.append(name+".setXMLAutoLoading(\""+getChildAction()+"\");\n");
		treeHtml.append(name+".enableThreeStateCheckboxes(true);\n");
		
		if(getChildAction().indexOf("?")>0)
			treeHtml.append(name+".loadXML(\""+getChildAction()+"&id="+rootId+"\");");
		else
			treeHtml.append(name+".loadXML(\""+getChildAction()+"?id="+rootId+"\");");
		if(onClickHandler!=null)
			treeHtml.append(name+".setOnClickHandler("+onClickHandler+");\n");
		if(onOpenHandler!=null)
			treeHtml.append(name+".setOnClickHandler("+onOpenHandler+");\n");
		if(onCheckHandler!=null)
			treeHtml.append(name+".setOnClickHandler("+onCheckHandler+");\n");
		if(onDblClickHandler!=null)
			treeHtml.append(name+".setOnClickHandler("+onDblClickHandler+");\n");
		treeHtml.append(name+".enableSmartXMLParsing(0);\n");
		treeHtml.append(name+".enableSmartCheckboxes(0);\n");
		treeHtml.append(name+".enableHighlighting(1);\n");
		treeHtml.append(name+".enableAutoTooltips(1);\n");
		treeHtml.append(name+".enableThreeStateCheckboxes(0);\n");
		treeHtml.append(name+".enableFixedMode(1);\n");			
		
		treeHtml.append("</script>");
		JspWriter writer = pageContext.getOut();
		try {		
			writer.println(treeHtml);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}

	public void setName(String name) {
		this.name = name;
		try {
			this.name=(String) ExpressionEvaluatorManager.evaluate("name",name,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setTreeBox(String treeBox) {
		this.treeBox = treeBox;
		try {
			this.treeBox=(String) ExpressionEvaluatorManager.evaluate("treeBox",treeBox,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setTheme(String theme) {
		this.theme = theme;
		try {
			this.theme=(String) ExpressionEvaluatorManager.evaluate("theme",theme,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public String getRootId() {
		return rootId;
	}

	public void setRootId(String rootId) {
		this.rootId = rootId;
		try {
			this.rootId=(String) ExpressionEvaluatorManager.evaluate("rootId",rootId,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setChildAction(String childAction) {
		this.childAction = childAction;
		try {
			this.childAction=(String) ExpressionEvaluatorManager.evaluate("childAction",childAction,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}	

	public void setOnClickHandler(String onClickHandler) {
		this.onClickHandler = onClickHandler;
		try {
			this.onClickHandler=(String) ExpressionEvaluatorManager.evaluate("onClickHandler",onClickHandler,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setOnOpenHandler(String onOpenHandler) {
		this.onOpenHandler = onOpenHandler;
		try {
			this.onOpenHandler=(String) ExpressionEvaluatorManager.evaluate("onOpenHandler",onOpenHandler,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setOnCheckHandler(String onCheckHandler) {
		this.onCheckHandler = onCheckHandler;
		try {
			this.onCheckHandler=(String) ExpressionEvaluatorManager.evaluate("onCheckHandler",onCheckHandler,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setOnDblClickHandler(String onDblClickHandler) {
		this.onDblClickHandler = onDblClickHandler;
		try {
			this.onDblClickHandler=(String) ExpressionEvaluatorManager.evaluate("onDblClickHandler",onDblClickHandler,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	

	public String getChildAction() {
		if(childAction==null)
			return "customTreeController.wit?rootid="+rootId+"&data="+data
			+"&datatype="+datatype+"&nodeid="+nodeid+"&nodelabel="+nodelabel
			+"&parentid="+parentid+"&others="+others
			+(data2==null?"":"&data2="+data2)
			+(nodeid2==null?"":"&nodeid2="+nodeid2)
			+(nodelabel2==null?"":"&nodelabel2="+nodelabel2);
		try {
			this.childAction=(String) ExpressionEvaluatorManager.evaluate("childAction",childAction,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		return childAction;
	}

	public void setData(String data) {
		this.data = data;
		try {
			this.data=(String) ExpressionEvaluatorManager.evaluate("data",data,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
		try {
			this.datatype=(String) ExpressionEvaluatorManager.evaluate("datatype",datatype,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setData2(String data2) {
		this.data2 = data2;
		try {
			this.data2=(String) ExpressionEvaluatorManager.evaluate("data2",data2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
		try {
			this.parentid=(String) ExpressionEvaluatorManager.evaluate("parentid",parentid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodeid(String nodeid) {
		this.nodeid = nodeid;
		try {
			this.nodeid=(String) ExpressionEvaluatorManager.evaluate("nodeid",nodeid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodelabel(String nodelabel) {
		this.nodelabel = nodelabel;
		try {
			this.nodelabel=(String) ExpressionEvaluatorManager.evaluate("nodelabel",nodelabel,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setOthers(String others) {
		this.others = others;
		try {
			this.others=(String) ExpressionEvaluatorManager.evaluate("others",others,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodeid2(String nodeid2) {
		this.nodeid2 = nodeid2;
		try {
			this.nodeid2=(String) ExpressionEvaluatorManager.evaluate("nodeid2",nodeid2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodelabel2(String nodelabel2) {
		this.nodelabel2 = nodelabel2;
		try {
			this.nodelabel2=(String) ExpressionEvaluatorManager.evaluate("nodelabel2",nodelabel2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

}
